package org.aiforum.web.action;


import org.aiforum.common.Response;
import org.aiforum.common.UserContant;
import org.aiforum.domain.User;
import org.aiforum.service.UserService;
import org.aiforum.web.common.AiActionContext;
import org.springframework.beans.factory.annotation.Autowired;


public class UserAction extends AiActionContext{
    /**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private User user=new User();
    
    //创建业务接口对象
    @Autowired
    private  UserService userService;
    
    /*
     * 处理用户登录请求
     * 
     */
    public String login(){
    	//调用用户业务接口进行登录
    	Response response=userService.login(user);
    	if(response.isState()){
    		//将缓存用户名添加到session中
    		setSession(UserContant.CURRENT_USER, response.getData());
    		//记录登录信息到日志中
    		logger.info(user.getAccount()+"login success.");
    		//登录成功返回首页
    		return "index";
    	}else{
    		//如果失败则返回登录页
    		//把失败消息添加到struts上下文
    		addActionError(response.getMessage());
    		return "login";
    	}
    }
    
    
    /*
     * 处理用户请求
     */
    public String register(){
    	//输出打印对象
    	System.out.println(user);
    	//调用业务接口处理注册
    	Response response =userService.register(user);
    	if(response.isState()){
    		//返回注册成功登录页面
    	  logger.info(user.getAccount()+"register success");
    	   return "root_login";
    	}else{
    		//注册失败返回注册页面
    		//将消息添加到struts2上下文
    		addActionMessage(response.getMessage());
    		return "register";
    	}
    }
    
    /*
     * 用户注销登录功能
     * 
     */
    public String logout(){
       //获取登录用户名
       String account=getSession(UserContant.CURRENT_USER, String.class);
       logger.info(user.getAccount()+"logout. ");
       //移除用户登录缓存
       userService.logout(account);
       //注销session
       invalidateSession();
       return "login";	
    }
    
    public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}



	
		
}
